package com.rc.evcharger.controller;

import com.rc.evcharger.common.base.TResponse;
import com.rc.evcharger.common.constant.HttpConstant;
import com.rc.evcharger.common.constant.SocketConstant;
import com.rc.evcharger.vo.charge.StartChargeReq;
import com.rc.evcharger.vo.charge.StopChargeReq;
import com.rc.evcharger.service.CommandService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * Create by Yang on 2021/8/3
 * std内部相互调用，不对外部接口
 */
@RestController
@RequestMapping("/std")
public class StdChargeController {
    private static Logger logger = LogManager.getLogger(StdChargeController.class);

    @Autowired
    private CommandService commandService;

    /**
     * 开始充电，微信启动充电没有外部订单号
     */
    @RequestMapping(value = "/query_start_charge", method = RequestMethod.POST)
    public TResponse query_start_charge(@RequestBody StartChargeReq tRequest) {
        try {
            logger.info("std.query_start_charge.request:{}", tRequest);
            //向桩发送充电指令
            Integer result = commandService.query_start_charge(tRequest);

            //如果不成功,返回各种错误码
            if (SocketConstant.SUCCESS == (result)) {
                return TResponse.SUCCESS();
            } else {
                switch (result) {
                    case SocketConstant.FAIL:
                        return TResponse.FAIL(SocketConstant.FAIL_SEND_CODE);
                    case SocketConstant.UNIQUE:
                        return TResponse.FAIL("发送指令重复");
                    case SocketConstant.EXCEPTION:
                        return TResponse.FAIL(HttpConstant.FAIL_CODE_501, "余额不足");
                    case SocketConstant.USING:
                        return TResponse.FAIL(HttpConstant.FAIL_CODE_502, "上次订单还没结算完，请稍等");
                    default:
                        return TResponse.FAIL("状态未知:" + result);
                }
            }
        } catch (Exception e) {
            logger.error("std.query_start_charge 异常", e);
            return TResponse.FAIL(SocketConstant.FAIL_SEND_CODE);
        }
    }


    /**
     * 停止充电
     */
    @RequestMapping(value = "/query_stop_charge", method = RequestMethod.POST)
    public TResponse query_stop_charge(@RequestBody StopChargeReq stopChargeReq) {
        try {
            logger.info("std.query_stop_charge.request:{}", stopChargeReq);
            if (stopChargeReq.getStopType() == null) {
                stopChargeReq.setStopType(SocketConstant.STOP_NORMAL_0X0);
            }
            //发送停止指令
            Integer result = commandService.query_stop_charge(stopChargeReq);
            if (SocketConstant.SUCCESS == (result)) {
                return TResponse.SUCCESS();
            } else {
                switch (result) {
                    case SocketConstant.FAIL:
                        return TResponse.FAIL(SocketConstant.FAIL_SEND_CODE);
                    case SocketConstant.UNIQUE:
                        return TResponse.FAIL("发送指令重复");
                    case SocketConstant.EXCEPTION:
                        return TResponse.FAIL(HttpConstant.FAIL_CODE_501, "余额不足");
                    case SocketConstant.USING:
                        return TResponse.FAIL(HttpConstant.FAIL_CODE_502, "上次订单还没结算完，请稍等");
                    default:
                        return TResponse.FAIL("状态未知:" + result);
                }
            }
        } catch (Exception e) {
            logger.error("std.query_stop_charge 异常", e);
            return TResponse.FAIL(SocketConstant.FAIL_SEND_CODE);
        }
    }
}
